<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mhrb:customer:add']"
        >新增客服</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="customerList">
      <el-table-column  label="客服名称" align="center" prop="customerName" />
      <el-table-column label="二维码" align="center" prop="qrCode" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.qrCode" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mhrb_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status === 1"
            @click="handleUpdate(scope.row,0)"
            v-hasPermi="['mhrb:platform:customer:edit']"
          ><span style="color:red">下线</span>
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status === 0"
            @click="handleUpdate(scope.row,1)"
            v-hasPermi="['mhrb:platform:customer:edit']"
          >上线
          </el-button>
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mhrb:platform:customer:remove']"
          ><span style="color:red">删除</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客服配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客服名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客服名称" />
        </el-form-item>
        <el-form-item label="二维码" prop="qrCode">
          <image-upload :limit="1" :fileSize="2" :fileType="['png','jpg','gif','jpeg']" v-model="form.qrCode"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.mhrb_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/mhrb/platform/customer";

export default {
  name: "Customer",
  dicts: ['mhrb_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客服配置表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qrCode: null,
        status: null,
        customerName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qrCode: [
          { required: true, message: "二维码不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        customerName: [
          { required: true, message: "客服名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客服配置列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客服";
    },
    /** 修改按钮操作 */
    handleUpdate(row, stauts) {
      const obj = {...row}
      obj.status = stauts
      updateCustomer(obj).then(res => {
        if (stauts) {
          this.$modal.msgSuccess("上线成功");
        } else {
          this.$modal.msg("下线成功");
        }
      })
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.customerId == null) {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerIds = row.customerId
      this.$modal.confirm('是否确认删除客服配置编号为"' + customerIds + '"的数据项？').then(function() {
        return delCustomer(customerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
